<?php

/**
 * 在我们后台跑的任务
 */
namespace backend\modules\tool\Job;

use backend\modules\tool\DataSource\SqlSource;
use backend\modules\tool\helpers\DbHelper;

class SqlNodeScript extends SqlSource
{
    protected $id;
    protected $batch_num=100;
    protected $run_fun="";
    protected $fun_name;
    protected $node;
    protected $target_pdo_conifg;
    protected $local_pdo_config;
    protected $before_sql;
    protected $sql;
    public function __construct()
    {


        $this->target_pdo=$this->getPdoConnect($this->target_pdo_conifg);
        $this->target_pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
        $this->local_pdo=$this->getPdoConnect($this->local_pdo_config);
        if(!empty($this->before_sql)){
            foreach (explode("--****--",$this->before_sql) as $item){
                if(!empty($item)){
                    $this->local_pdo->exec($item);
                }
            }
        }
        parent::__construct();
    }
    protected function setRunFun(){
//        return function ($data){
//
//        };
    }
    protected function getPdoConnect($config){
        return DbHelper::GetPdo($config["driver"],$config["host"],$config["user"],$config["password"],$config["port"],$config["database"]);
    }
    public function StoreData($data)
    {
        if(!empty(($fun=$this->setRunFun()))){
            $data=call_user_func($fun,$data);
        }
//        echo "TaskName".$this->node->node_name.PHP_EOL;
        echo microtime(true)-start_at.PHP_EOL;
        parent::StoreData($data); // TODO: Change the autogenerated stub
    }
}